package com.bbzn.device.client.service.emq.data;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.dataobject.FenceDevice;
import com.bbzn.device.client.service.EmqStrategy;
import com.bbzn.device.client.service.FenceDeviceService;
import com.bbzn.device.client.utils.OptionalBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author: wulongbo
 * @Date : 2021/4/9 11:19
 * @Version 1.0
 */
@Component
public class EmqDelFenceStrategy implements EmqStrategy {

    @Resource
    private FenceDeviceService fenceDeviceService;

    // 删除犯人与围栏关联关系
    @Override
    public String emqAction(String payLoad) {
        try {
            JSONObject jsonObject = JSON.parseObject(payLoad);
            String imei = jsonObject.getString("imei");
            //围栏和设备的关联id
            Long id = jsonObject.getLong("id");
            if(!OptionalBean.ofNullable(imei).isPresent()){
                return "json解析错误";
            }
            if(!OptionalBean.ofNullable(id).isPresent()){
                return "json解析错误";
            }

            FenceDevice fenceDevice = new FenceDevice();
            fenceDevice.setId(id);
            fenceDevice.setImei(imei);
            fenceDeviceService.deleteSelective(fenceDevice);

        } catch (Exception e) {
            return "删除犯人与围栏关联关系失败！";
        }
        return "删除犯人与围栏关联关系成功！";
    }
}
